package comp353Games;

import javax.swing.*;
import javax.swing.BoxLayout;
import java.awt.*;

/**
 * Opener.java is an introductory GUI designed to allow the user to select a
 * game to play.
 *
 * @author Andre Harvey, Tyler Radley, and Deanna Fink
 * @version 2/27/12
 */
public class Opener2 extends JFrame {

    private JToggleButton gameSumButton;
    private JButton toadsAndFrogsButton;
    private JButton nimButton;
    private JButton snortButton;
    private JButton clobberButton;
    private JButton exitButton;
    private JLabel welcomeLabel;
    private JLabel questionLabel;
    private JPanel openerPanel;
    private boolean isGameSum = false;
    private int numGames = 2;
    private int index = 0;
    private GeneralGame[] gameSum = new GeneralGame[numGames];
    private int i = 0;
    /**
     * Constructor which initializes the Opener.
     *
     */
    public Opener2() {
        super("Awesome Game Program");
        initComponents();
    }

    /**
     * Initial components used to construct the Opener
     */
    private void initComponents() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        openerPanel = new JPanel();
        openerPanel.setPreferredSize(new Dimension(500, 200));
        openerPanel.setLayout(new BoxLayout(openerPanel, BoxLayout.PAGE_AXIS));

        welcomeLabel = new JLabel();
        welcomeLabel.setFont(new java.awt.Font("Vijaya", 1, 24));
        welcomeLabel.setText("Welcome to the Game Suite");
        welcomeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        questionLabel = new JLabel();
        questionLabel.setFont(new java.awt.Font("Vijaya", 0, 18)); // NOI18N
        questionLabel.setText("Which game would you like to play?");
        questionLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        exitButton = new JButton();
        exitButton.setText("Exit");
        exitButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        exitButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                exitButtonActionPerformed(event);
            }
        });


        openerPanel.add(welcomeLabel);
        openerPanel.add(new Box.Filler(new Dimension(500, 30),
                new Dimension(500, 30),
                new Dimension(500, 30)));
        openerPanel.add(questionLabel);
        openerPanel.add(new Box.Filler(new Dimension(500, 50),
                new Dimension(500, 50),
                new Dimension(500, 50)));
        openerPanel.add(this.createButtonPane());
        openerPanel.add(new Box.Filler(new Dimension(500, 5),
                new Dimension(500, 5),
                new Dimension(500, 5)));
        openerPanel.add(exitButton);
        openerPanel.add(new Box.Filler(new Dimension(500, 50),
                new Dimension(500, 50),
                new Dimension(500, 50)));

        this.add(openerPanel);

        pack();
        repaint();
    }

    /**
     * JPanel which holds the buttons to start the available games.
     *
     * @return the JPanel which holds the buttons to start the available games.
     */
    private JPanel createButtonPane() {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setAlignmentX(Component.CENTER_ALIGNMENT);

        gameSumButton = new JToggleButton();
        toadsAndFrogsButton = new JButton();
        nimButton = new JButton();
        snortButton = new JButton();
        clobberButton = new JButton();

        gameSumButton.setText("Game Sum");
        gameSumButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                isGameSum = true;
            }
        });

        toadsAndFrogsButton.setText("Toads And Frogs");
        toadsAndFrogsButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                toadsAndFrogsButtonActionPerformed(event);
            }
        });

        nimButton.setText("Nim");
        nimButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                nimButtonActionPerformed(event);
            }
        });

        snortButton.setText("Snort");
        snortButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                snortButtonActionPerformed(event);
            }
        });

        clobberButton.setText("Clobber");
        clobberButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent event) {
                clobberButtonActionPerformed(event);
            }
        });

        buttonPane.add(gameSumButton);
        buttonPane.add(nimButton);
        buttonPane.add(toadsAndFrogsButton);
        buttonPane.add(snortButton);
        buttonPane.add(clobberButton);

        return buttonPane;
    }

    private void gameSumButtonActionPerformed(java.awt.event.ActionEvent event) {
        this.isGameSum = true;
        dispose();
        while (i < numGames-1){
            System.out.println(i);
            new Thread(new Runnable(){
                public void run(){
                    
                    GeneralController2 controller = new GeneralController2(gameSum);
                    controller.GeneralController2();
                }           
            }).start();
            i++;
        }
    }
    /**
     * Opens Toads and Frogs game.
     *
     * @param event The mouse click that prompts for the opening of the game.
     */
    private void toadsAndFrogsButtonActionPerformed(java.awt.event.ActionEvent event) {
        if (isGameSum) {
            if (index < numGames) {
                gameSum[index] = new ToadsAndFrogs();
                index++;
                if (index == numGames){
                    gameSumButtonActionPerformed(event);
                }
            }
        } else if (!isGameSum) {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    ToadsAndFrogs game0 = new ToadsAndFrogs();
                    GeneralController controller = new GeneralController(game0);
                    controller.GeneralController();
                }
            }).start();
        }
    }

    /**
     * Opens Nim game.
     *
     * @param event The mouse click that prompts for the opening of the game.
     */
    private void nimButtonActionPerformed(java.awt.event.ActionEvent event) {
         if (isGameSum) {
            if (index < numGames) {
                gameSum[index] = new Nim();
                index++;
                if (index == numGames){
                    gameSumButtonActionPerformed(event);
                }
            }
        } else if (!isGameSum) {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    Nim game0 = new Nim();
                    GeneralController controller = new GeneralController(game0);
                    controller.GeneralController();
                }
            }).start();
        }
    }

    /**
     * Opens Snort game.
     *
     * @param event The mouse click that prompts for the opening of the game.
     */
    private void snortButtonActionPerformed(java.awt.event.ActionEvent event) {
         if (isGameSum) {
            if (index < numGames) {
                gameSum[index] = new Snort();
                index++;
                if (index == numGames){
                    gameSumButtonActionPerformed(event);
                }
            }
        } else if (!isGameSum) {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    Snort game0 = new Snort();
                    GeneralController controller = new GeneralController(game0);
                    controller.GeneralController();
                }
            }).start();
        }
    }

    /**
     * Opens Clobber game.
     *
     * @param event The mouse click that prompts for the opening of the game.
     */
    private void clobberButtonActionPerformed(java.awt.event.ActionEvent event) {
        if (isGameSum) {
            if (index < numGames) {
                gameSum[index] = new Clobber();
                index++;
                if (index == numGames){
                    gameSumButtonActionPerformed(event);
                }
            }
        } else if (!isGameSum) {
            dispose();
            new Thread(new Runnable() {

                public void run() {
                    Clobber game0 = new Clobber();
                    GeneralController controller = new GeneralController(game0);
                    controller.GeneralController();
                }
            }).start();
        }
    }


    /**
     * Exits the Opener.
     *
     * @param event The mouse click that prompts Opener to exit.
     */
    private void exitButtonActionPerformed(java.awt.event.ActionEvent event) {
        System.exit(0);
    }

    /**
     * Main method for testing.
     *
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Opener2().setVisible(true);
            }
        });
    }
}
